/*
 * @Author: wuqingchun
 * @Date: 2024-07-23 13:24:39
 * @Description: 中心缓存类
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-07-26 09:04:47
 */
#ifndef __CENTRAL_CACHE_H__
#define __CENTRAL_CACHE_H__


#include <cstddef>
#include <mutex>

#include "common.h"
#include "span.h"



/**
 * @brief 中心缓存类(单例)
 * 一个进程只会存在一个该类的对象，进程中多个线程共享该对象
 * 
 */
class CentralCache
{
private:
    static constexpr size_t NR_FREELIST = 208;
public:

    static CentralCache* GetInstance()
    {
        return &m_objInstance;
    }
    
    /**
     * @brief 获取一定数量的对象
     * 
     * @param refStartPtr 
     * @param refEndPtr 
     * @param ulObjNum 
     * @param ulObjSize 
     * @return size_t 
     */
    size_t FetchRangeObj(void*& refStartPtr, void*& refEndPtr, size_t ulObjNum, size_t ulObjSize);

private:
    CentralCache()
    {

    }

    size_t __AllocObjectsFromList(size_t ulIndex, size_t ulObjNum, void*& refStartPtr, void*& refEndPtr);

    size_t __AllocObjectsFromPGCache(size_t ulIndex, size_t ulObjNum, size_t ulObjSize, void*& refStartPtr, void*& refEndPtr);

    size_t __GetObjectsFromSpan(Span& refSpan, size_t ulObjNum, void*& refStartPtr, void*& refEndPtr);
private:
    static CentralCache m_objInstance;

    SpanList m_arrSpanList[SizeAlignStrategy::NR_FREELIST];
    std::mutex m_arrMtx[SizeAlignStrategy::NR_FREELIST];
};

#endif //__CENTRAL_CACHE_H__